package 补充.多线程;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class Ticket3 implements Runnable{
    private int ticket=5;
    private final Lock lock=new ReentrantLock();
    @Override
    public void run() {
        lock.lock();
        while(true){
                if(ticket>0){
                    try {
                        Thread.sleep(100);
                        System.out.println(Thread.currentThread().getName()+"卖出第"+ticket+"张票，还剩"+--ticket+"张票");
                    }catch(InterruptedException e){
                        e.printStackTrace();
                    }finally {
                        lock.unlock();
                    }
                }
        }
    }
}
public class lock机制 {
    public static void main(String[] args) {
        Ticket1 ticket = new Ticket1();
        Thread t1 = new Thread(ticket,"窗口一");
        Thread t2 = new Thread(ticket,"窗口二");
        Thread t3 = new Thread(ticket,"窗口三");
        t1.start();
        t2.start();
        t3.start();
    }
}
